package com.hyt.it.ogt.kq.service.gov.service.impl;

import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.kq.common.config.ConfigManager;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.common.gov.constant.StringCodeEnum;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.service.gov.feign.tps.GovTpsClient;
import com.hyt.it.ogt.kq.service.gov.mapper.TaskTestInventoryMapper;
import com.hyt.it.ogt.kq.service.gov.model.entity.OfficeTestInventory;
import com.hyt.it.ogt.kq.service.gov.model.entity.TaskTestInventory;
import com.hyt.it.ogt.kq.service.gov.model.vo.obs.HuaweiObsCopyObjectResultVO;
import com.hyt.it.ogt.kq.service.gov.service.IOfficeTestInventoryService;
import com.hyt.it.ogt.kq.service.gov.service.ITaskTestInventoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Optional;

/**
 * <p>
 *  任务测试项清单表 服务实现类
 * </p>
 *
 * @author huangyh
 * @since 2022-04-01
 */
@Service
public class TaskTestInventoryServiceImpl extends BaseServiceImpl<TaskTestInventoryMapper, TaskTestInventory> implements ITaskTestInventoryService {

    @Resource
    private IOfficeTestInventoryService iOfficeTestInventoryService;

    @Resource
    private ConfigManager configManager;

    @Resource
    private GovTpsClient tpsClient;

    /**
     * 路径统一分隔符
     */
    public static final String SEPARATOR = "/";

    @Override
    public void init(String taskId, String officeId) {
        //获取机构测试项清单数据，若机构下暂无测试项清单数据，则获取系统测试项清单数据
        OfficeTestInventory officeTestInventory = iOfficeTestInventoryService.getByOfficeId(officeId);
        String officeTestInventoryPath = Optional.ofNullable(officeTestInventory)
                .map(OfficeTestInventory::getPath)
                .orElseGet(() -> iOfficeTestInventoryService.init(officeId));
        //复制测试项清单数据至考试任务中
        String taskTestInventoryPath = this.getTaskTestInventoryPath(taskId);
        ApiResponse<HuaweiObsCopyObjectResultVO> response =
                tpsClient.copyHuaweiObsObject(officeId, officeTestInventoryPath, taskTestInventoryPath);
        HuaweiObsCopyObjectResultVO huaweiObsCopyObjectResultVO = response.getData();
        int statusCode = Optional.ofNullable(huaweiObsCopyObjectResultVO)
                .map(HuaweiObsCopyObjectResultVO::getStatusCode)
                .orElse(0);
        if (response.getCode() != 1
                || statusCode != HttpServletResponse.SC_OK) {
            throw new KqException(ResponseCode.TASK_INIT_FAIL.getCode(),
                    "复制机构测试项清单数据至考试任务中失败");
        }
        //保存任务测试项清单数据
        TaskTestInventory taskTestInventory = new TaskTestInventory();
        taskTestInventory.setTaskId(taskId)
                .setPath(taskTestInventoryPath);
        if (!this.save(taskTestInventory)) {
            throw new KqException(ResponseCode.TASK_INIT_FAIL.getCode(),
                    "插入任务测试项清单表失败");
        }
    }

    @Override
    public String getTaskTestInventoryPath(String taskId) {
        //任务级文件存储规则：“根目录/任务编号/业务功能目录/具体的文件.xxx”
        String cloudRootPath = configManager.getCloudRootPath();
        cloudRootPath = cloudRootPath.substring(0, cloudRootPath.length() - 1);
        return StringUtils.joinWith(SEPARATOR, cloudRootPath, taskId,
                configManager.getTestInventoryPath(),
                StringCodeEnum.TEST_INVENTORY_FILE_NAME.getCode());
    }

    @Override
    public TaskTestInventory getByTaskCode(String taskCode) {
        return baseMapper.getByTaskCode(taskCode);
    }

}
